In [1]:
import sympy
In [2]:
sympy.init_printing()
In [3]:
x = sympy.Symbol('x')
In [4]:
sympy.expand((x + 2)**4)
Out[4]:
$$x^{4} + 8 x^{3} + 24 x^{2} + 32 x + 16$$
In [8]:
a = sympy.solve(x**2 + 2, x)
In [9]:
len(a)
Out[9]:
$$2$$
In [10]:
[sympy.N(e) for e in a]
Out[10]:
$$\left [ - 1.4142135623731 i, \quad 1.4142135623731 i\right ]$$
In [11]:
list(map(sympy.N, a))
Out[11]:
$$\left [ - 1.4142135623731 i, \quad 1.4142135623731 i\right ]$$
In [14]:
sympy.laplace_transform?
In [15]:
s, t = sympy.symbols('s t')
In [16]:
sympy.laplace_transform(sympy.sin(t) + 5, t, s, noconds=True)
Out[16]:
$$\frac{5 s^{2} + s + 5}{s \left(s^{2} + 1\right)}$$
In [17]:
def L(g):
    return sympy.laplace_transform(g, t, s, noconds=True)
In [18]:
L(sympy.sin(t) + 5)
Out[18]:
$$\frac{5 s^{2} + s + 5}{s \left(s^{2} + 1\right)}$$
In [19]:
sympy.inverse_laplace_transform(1/(s + 1), s, t)
Out[19]:
$$e^{- t} \theta\left(t\right)$$
In [20]:
G = (s + 1)/((s+2)*(s+3)*(s+4))
In [21]:
type(G)
Out[21]:
sympy.core.mul.Mul
In [22]:
s = sympy.Symbol('s')
t = sympy.Symbol('t')
In [23]:
t = sympy.Symbol('t', positive=True)
In [24]:
variables = [s, t]
In [25]:
mu = sympy.Symbol('s')
In [26]:
mu
Out[26]:
$$s$$
In [27]:
for varaiable in variables:
    print(variable)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-27-25d0663afbe9> in <module>()
      1 for varaiable in variables:
----> 2     print(variable)

NameError: name 'variable' is not defined
In [28]:
sympy.inverse_laplace_transform(G, s, t)
Out[28]:
$$\frac{1}{2 e^{4 t}} \left(- e^{2 t} + 4 e^{t} - 3\right)$$
In [29]:
sympy.apart(G)
Out[29]:
$$- \frac{3}{2 s + 8} + \frac{2}{s + 3} - \frac{1}{2 s + 4}$$
In [30]:
sympy.solve(t**2 + 2, t)
Out[30]:
$$\left [ \right ]$$
In [61]:
from sympy.abc import a, b, c
In [62]:
eq1 = a + b - c
eq1
Out[62]:
$$a + b - c$$
In [90]:
eq2 = 2*a + b - 5*c*b
eq2
Out[90]:
$$2 a - 5 b c + b$$
In [91]:
result = sympy.solve(eq2, b)
In [92]:
result
Out[92]:
$$\left [ \frac{2 a}{5 c - 1}\right ]$$
In [ ]:
solb = result[0]
solb, = result
(solb,) = result
[solb] = result
In [84]:
eq1.subs({b: result[0]})
Out[84]:
$$a + \frac{2 a}{5 c - 1} - c$$
In [93]:
result = sympy.solve([eq1, eq2], [a, b])
In [94]:
result
Out[94]:
$$\left \{ a : \frac{c \left(5 c - 1\right)}{5 c + 1}, \quad b : \frac{2 c}{5 c + 1}\right \}$$
In [85]:
d = {}
In [ ]:
d['name1'] = 1
d['name1'] = 2
In [73]:
d = {'name1': 1,
     'name1': 2}
In [74]:
d['name1']
Out[74]:
$$2$$
In [36]:
lst = [1, 2, 3]
In [37]:
type(lst)
Out[37]:
list
In [38]:
tup = 1, 2, 3
In [39]:
type(tup)
Out[39]:
tuple
In [40]:
tup2 =(((((1, 2, 3)))))
In [41]:
type(tup2)
Out[41]:
tuple
In [42]:
tup, tup2
Out[42]:
$$\left ( \left ( 1, \quad 2, \quad 3\right ), \quad \left ( 1, \quad 2, \quad 3\right )\right )$$
In [43]:
a, (b, c) = 1, [2, 3]
In [44]:
def f(x):
    return x, x+2
In [45]:
c = f(x)
In [46]:
t = 1, 2, 3, 4, 5, 6
In [47]:
b, *bs = t
In [48]:
b
Out[48]:
$$1$$
In [49]:
bs
Out[49]:
$$\left [ 2, \quad 3, \quad 4, \quad 5, \quad 6\right ]$$
In [50]:
def myfunction(many, many1, many2, params):
    print(many, many1, many2, params)
In [51]:
lst = [1, 2, 3, 4]
In [52]:
myfunction(5, *lst[1:])
5 2 3 4
In [53]:
def myfunction(*args):
    print(args)
In [54]:
myfunction(1, 2, 3)
(1, 2, 3)
In [112]:
s = sympy.Symbol('s')
t = sympy.Symbol('t')
In [165]:
def G(s):
    return 1/(s + 1)
    #return (s + 2)/(s**2 + 0.25*s + 1)
In [166]:
G(1)
Out[166]:
$$0.5$$
In [167]:
sympy.solve(sympy.denom(G(s)), s)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-167-0fb4ff7ceec1> in <module>()
----> 1 sympy.solve(sympy.denom(G(s)), s)

/Users/alchemyst/anaconda3/lib/python3.5/site-packages/sympy/simplify/radsimp.py in denom(expr)
    983
    984 def denom(expr):
--> 985     return fraction(expr)[1]
    986
    987

/Users/alchemyst/anaconda3/lib/python3.5/site-packages/sympy/simplify/radsimp.py in fraction(expr, exact)
    948
    949     """
--> 950     expr = sympify(expr)
    951
    952     numer, denom = [], []

/Users/alchemyst/anaconda3/lib/python3.5/site-packages/sympy/core/sympify.py in sympify(a, locals, convert_xor, strict, rational, evaluate)
    280         try:
    281             return type(a)([sympify(x, locals=locals, convert_xor=convert_xor,
--> 282                 rational=rational) for x in a])
    283         except TypeError:
    284             # Not all iterables are rebuildable with their type.

ValueError: sequence too large; cannot be greater than 32
In [ ]:
f = sympy.inverse_laplace_transform(G(s), s, t)
In [168]:
sympy.plot(f, (t, 0.1, 10))
Out[168]:
<sympy.plotting.plot.Plot at 0x10ff52710>
In [137]:
import numpy
In [169]:
omega = numpy.logspace(-1, 2)
In [170]:
s = 1j*omega
In [175]:
mag = numpy.abs(G(s))
phase = numpy.angle(G(s))
In [176]:
import matplotlib.pyplot as plt
In [177]:
%matplotlib notebook
In [178]:
plt.subplot(2, 1, 1)
plt.loglog(omega, mag)
plt.subplot(2, 1, 2)
plt.semilogx(omega, phase)
Out[178]:
[<matplotlib.lines.Line2D at 0x110aaa1d0>]
In [181]:
plt.figure()
plt.plot(G(s).real, G(s).imag)
plt.plot(G(s).real, -G(s).imag)
plt.axis('equal')
Out[181]:
$$\left ( 0.0, \quad 1.0, \quad -0.6, \quad 0.6\right )$$